/**
 ******************************************************************************
 * @file           : bsp_iic_config.h
 * @brief          : Hardware I2C Configuration File (STM32F4xx)
 ******************************************************************************
 * @attention
 *
 * 硬件I2C配置文件（基于STM32 HAL库）
 * STM32F4xx使用GPIO AF复用功能
 *
 ******************************************************************************
 */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __BSP_IIC_CONFIG_H
#define __BSP_IIC_CONFIG_H

#ifdef __cplusplus
extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include "stm32f4xx.h"

/* Exported types ------------------------------------------------------------*/
/* Exported constants --------------------------------------------------------*/

/* ============================================================================ */
/*                       日志系统配置（改这里的数字）                             */
/* ============================================================================ */
/* 日志后端选择：1=EasyLogger, 2=printf, 0=关闭 */
#define IIC_USE_LOG     1

/* 根据配置自动定义日志宏 */
#if (IIC_USE_LOG == 1)
    /* 使用EasyLogger */
    #ifndef LOG_TAG
        #define LOG_TAG    "IIC"
    #endif
    #include "elog.h"
    
#elif (IIC_USE_LOG == 2)
    /* 使用printf */
    #include <stdio.h>
    #define log_i(fmt, ...)     printf(fmt "\r\n", ##__VA_ARGS__)
    #define log_w(fmt, ...)     printf("[WARN] " fmt "\r\n", ##__VA_ARGS__)
    #define log_e(fmt, ...)     printf("[ERROR] " fmt "\r\n", ##__VA_ARGS__)
    #define log_d(fmt, ...)     printf("[DEBUG] " fmt "\r\n", ##__VA_ARGS__)
    
#else
    /* 关闭日志 */
    #define log_i(fmt, ...)
    #define log_w(fmt, ...)
    #define log_e(fmt, ...)
    #define log_d(fmt, ...)
#endif

/* ============================================================================ */
/*                          用户配置区（修改这里的数字）                          */
/* ============================================================================ */

/**
 * @brief I2C外设选择
 * 
 * 可选值：1~3
 *   1 = I2C1
 *   2 = I2C2
 *   3 = I2C3
 */
#define IIC_SELECT_NUM      1       /*!< I2C外设选择（1~3） */

/**
 * @brief I2C引脚选择
 * 
 * 可选值：0/1
 *   0 = 默认引脚组
 *   1 = 备用引脚组
 * 
 * 引脚映射表（STM32F4xx）：
 *   I2C1: SCL=PB6, SDA=PB7 (AF4)  或  SCL=PB8, SDA=PB9 (AF4)
 *   I2C2: SCL=PB10, SDA=PB11 (AF4) 或 SCL=PF1, SDA=PF0 (AF4)
 *   I2C3: SCL=PA8, SDA=PC9 (AF4)   或 SCL=PH7, SDA=PH8 (AF4)
 */
#define IIC_PIN_SELECT      0       /*!< 引脚选择 */

/**
 * @brief I2C速度配置（单位：Hz）
 * 
 * 标准模式：100000 (100kHz)
 * 快速模式：400000 (400kHz)
 */
#define IIC_SPEED           100000  /*!< I2C通信速度 */

/**
 * @brief I2C地址模式
 * 
 * 可选值：
 *   7  = 7位地址模式
 *   10 = 10位地址模式
 */
#define IIC_ADDR_MODE       7       /*!< I2C地址位数 */

/**
 * @brief I2C超时时间（毫秒）
 */
#define IIC_TIMEOUT_MS      1000    /*!< I2C操作超时时间 */

/* ============================================================================ */
/*                    自动配置区域（请勿手动修改）                                */
/* ============================================================================ */

/* --------------------------------------------------------------------------- */
/* I2C1 配置 (PB6/PB7 或 PB8/PB9)  AF4                                         */
/* --------------------------------------------------------------------------- */
#if (IIC_SELECT_NUM == 1)
    #define IIC_I2Cx                        I2C1
    #define IIC_I2Cx_CLK_ENABLE()           __HAL_RCC_I2C1_CLK_ENABLE()
    #define IIC_I2Cx_FORCE_RESET()          __HAL_RCC_I2C1_FORCE_RESET()
    #define IIC_I2Cx_RELEASE_RESET()        __HAL_RCC_I2C1_RELEASE_RESET()
    #define IIC_GPIO_AF                     GPIO_AF4_I2C1
    
    #if (IIC_PIN_SELECT == 0)  /* PB6/PB7 */
        #define IIC_SCL_GPIO_PORT               GPIOB
        #define IIC_SCL_GPIO_PIN                GPIO_PIN_6
        #define IIC_SCL_GPIO_CLK_ENABLE()       __HAL_RCC_GPIOB_CLK_ENABLE()
        
        #define IIC_SDA_GPIO_PORT               GPIOB
        #define IIC_SDA_GPIO_PIN                GPIO_PIN_7
        #define IIC_SDA_GPIO_CLK_ENABLE()       __HAL_RCC_GPIOB_CLK_ENABLE()
    #else  /* PB8/PB9 */
        #define IIC_SCL_GPIO_PORT               GPIOB
        #define IIC_SCL_GPIO_PIN                GPIO_PIN_8
        #define IIC_SCL_GPIO_CLK_ENABLE()       __HAL_RCC_GPIOB_CLK_ENABLE()
        
        #define IIC_SDA_GPIO_PORT               GPIOB
        #define IIC_SDA_GPIO_PIN                GPIO_PIN_9
        #define IIC_SDA_GPIO_CLK_ENABLE()       __HAL_RCC_GPIOB_CLK_ENABLE()
    #endif

/* --------------------------------------------------------------------------- */
/* I2C2 配置 (PB10/PB11 或 PF1/PF0)  AF4                                       */
/* --------------------------------------------------------------------------- */
#elif (IIC_SELECT_NUM == 2)
    #define IIC_I2Cx                        I2C2
    #define IIC_I2Cx_CLK_ENABLE()           __HAL_RCC_I2C2_CLK_ENABLE()
    #define IIC_I2Cx_FORCE_RESET()          __HAL_RCC_I2C2_FORCE_RESET()
    #define IIC_I2Cx_RELEASE_RESET()        __HAL_RCC_I2C2_RELEASE_RESET()
    #define IIC_GPIO_AF                     GPIO_AF4_I2C2
    
    #if (IIC_PIN_SELECT == 0)  /* PB10/PB11 */
        #define IIC_SCL_GPIO_PORT               GPIOB
        #define IIC_SCL_GPIO_PIN                GPIO_PIN_10
        #define IIC_SCL_GPIO_CLK_ENABLE()       __HAL_RCC_GPIOB_CLK_ENABLE()
        
        #define IIC_SDA_GPIO_PORT               GPIOB
        #define IIC_SDA_GPIO_PIN                GPIO_PIN_11
        #define IIC_SDA_GPIO_CLK_ENABLE()       __HAL_RCC_GPIOB_CLK_ENABLE()
    #else  /* PF1/PF0 */
        #define IIC_SCL_GPIO_PORT               GPIOF
        #define IIC_SCL_GPIO_PIN                GPIO_PIN_1
        #define IIC_SCL_GPIO_CLK_ENABLE()       __HAL_RCC_GPIOF_CLK_ENABLE()
        
        #define IIC_SDA_GPIO_PORT               GPIOF
        #define IIC_SDA_GPIO_PIN                GPIO_PIN_0
        #define IIC_SDA_GPIO_CLK_ENABLE()       __HAL_RCC_GPIOF_CLK_ENABLE()
    #endif

/* --------------------------------------------------------------------------- */
/* I2C3 配置 (PA8/PC9 或 PH7/PH8)  AF4                                         */
/* --------------------------------------------------------------------------- */
#elif (IIC_SELECT_NUM == 3)
    #define IIC_I2Cx                        I2C3
    #define IIC_I2Cx_CLK_ENABLE()           __HAL_RCC_I2C3_CLK_ENABLE()
    #define IIC_I2Cx_FORCE_RESET()          __HAL_RCC_I2C3_FORCE_RESET()
    #define IIC_I2Cx_RELEASE_RESET()        __HAL_RCC_I2C3_RELEASE_RESET()
    #define IIC_GPIO_AF                     GPIO_AF4_I2C3
    
    #if (IIC_PIN_SELECT == 0)  /* PA8/PC9 */
        #define IIC_SCL_GPIO_PORT               GPIOA
        #define IIC_SCL_GPIO_PIN                GPIO_PIN_8
        #define IIC_SCL_GPIO_CLK_ENABLE()       __HAL_RCC_GPIOA_CLK_ENABLE()
        
        #define IIC_SDA_GPIO_PORT               GPIOC
        #define IIC_SDA_GPIO_PIN                GPIO_PIN_9
        #define IIC_SDA_GPIO_CLK_ENABLE()       __HAL_RCC_GPIOC_CLK_ENABLE()
    #else  /* PH7/PH8 */
        #define IIC_SCL_GPIO_PORT               GPIOH
        #define IIC_SCL_GPIO_PIN                GPIO_PIN_7
        #define IIC_SCL_GPIO_CLK_ENABLE()       __HAL_RCC_GPIOH_CLK_ENABLE()
        
        #define IIC_SDA_GPIO_PORT               GPIOH
        #define IIC_SDA_GPIO_PIN                GPIO_PIN_8
        #define IIC_SDA_GPIO_CLK_ENABLE()       __HAL_RCC_GPIOH_CLK_ENABLE()
    #endif

#else
    #error "Invalid IIC_SELECT_NUM, please select 1~3"
#endif

/* Exported macro ------------------------------------------------------------*/
/* Exported variables --------------------------------------------------------*/
/* Exported functions --------------------------------------------------------*/

#ifdef __cplusplus
}
#endif

#endif /* __BSP_IIC_CONFIG_H */
